package com.yilin.tms.data.service.impl;

import com.yilin.commons.util.DateTimeUtil;
import com.yilin.commons.util.ImageEditUtils;
import com.yilin.commons.util.SharedImageUtils;
import com.yilin.commons.util.StringUtil;
import com.yilin.tms.core.entity.order.goods.GoodsOrder;
import com.yilin.tms.core.entity.order.transit.TransitOrder;
import com.yilin.tms.core.entity.system.system.AppIcon;
import com.yilin.tms.core.entity.truck.TruckVehicle;
import com.yilin.tms.core.entity.user.UserType;
import com.yilin.tms.core.entity.user.account.Account;
import com.yilin.tms.core.entity.user.organize.Organize;
import com.yilin.tms.data.service.ISharePostersService;
import com.yilin.tms.core.remote.ISystemRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;

@Service
public class SharePostersService implements ISharePostersService {

    @Autowired
    private ISystemRemote systemRemote;

    public static void main(String[] args) {
        Account account = new Account();
        account.setUserType(UserType.shipperUser);
        account.setName("杨海苹");
        // ------------------------货源---------------------------

//
//        GoodsOrder goodsOrder = new GoodsOrder();
//        goodsOrder.setConsignorArea("贵阳市");
//        goodsOrder.setConsigneeArea("黔东南苗");
//        goodsOrder.setKilometer(1026.0);
//        goodsOrder.setGoodsTypeName("水果");
//        goodsOrder.setShipperTransportCost(52.03);
//        goodsOrder.setCreateTime(new Date());
//        goodsOrder.setTitle("干线普货运输");
//        goodsSHarePosters(goodsOrder, account);
//        String str = "贵州省-贵阳市-清镇市";
//        System.out.println(str.substring(str.indexOf("-") + 1, str.lastIndexOf("-")));
        //------------------------运单---------------------------
//        TransitOrder transitOrder=new TransitOrder();
//        transitOrder.setTransportNo("TD1670231966671962");
//        transitOrder.setConsignorArea("贵阳你市");
//        transitOrder.setConsigneeArea("黔东南苗");
//        transitOrder.setShipperTransportCost(456.32);
//        transitSHarePosters( transitOrder,account);
        //------------------------车辆---------------------------
//        Vehicle vehicle=new Vehicle();
//        vehicle.setVehicleImageUrl("http://117.187.230.142:9000/truck/longDistanceCart_6m8/highHurdle/0.png");
//        vehicle.setTrackNo("贵AG650L");
//        vehicle.setTruckTypeName("轻型厢式货车");
//        vehicleSHarePosters( vehicle,account);
        //-----------------------------------------------
//        Organize organize=new Organize();
//        organize.setOrganizeLogo("https://picsum.photos/200/300");
//        organize.setOrganizeName("小镖人团队");
//        organize.setUserType(UserType.shipperUser);
//        accountInfoSHarePosters( organize,account);
    }

    //获取货源海报
    @Override
    public String goodsSHarePosters(GoodsOrder goodsOrder, Account account) {
        try {
            // 获取类所在项目路径，获取方式有多种
            String address = getClass().getResource("/images/").getPath();
            //生成二维码
            BufferedImage qrcodeImage = SharedImageUtils.qrcodeLogo("http://www.manongjc.com/detail/35-vqsxfzqcandcbgw.html", address + "goods_logo.png", 180, 180);
            // 1. 通过背景图片构建 BufferedImage
            BufferedImage mergeImage = ImageIO.read(new File(address + "goods_bg.png"));
            mergeImage = SharedImageUtils.drawTextInImage(mergeImage, account.getUserType().name + "" + account.getName(), 450, 1100, new Color(64, 47, 169), 80, Font.BOLD, "黑体");
            mergeImage = SharedImageUtils.drawTextInImage(mergeImage, StringUtil.getStringFirstAndEnd(areaCity(goodsOrder.getDisplayConsignorArea()), 2, 1, "..."), 420, 1550, new Color(0, 0, 0), 140, "华文行楷");
            mergeImage = SharedImageUtils.drawTextInImage(mergeImage, goodsOrder.getTitle(), 1050, 1620, new Color(79, 137, 244), 60, true, false, "华文行楷");
            mergeImage = SharedImageUtils.drawTextInImage(mergeImage, StringUtil.getStringFirstAndEnd(areaCity(goodsOrder.getDisplayConsigneeArea()), 2, 1, "..."), 1350, 1550, new Color(0, 0, 0), 140, "华文行楷");
            mergeImage = SharedImageUtils.drawTextInImage(mergeImage, goodsOrder.getGoodsTypeName(), 1050, 1950, new Color(107, 107, 107), 120, "华文行楷");
            mergeImage = SharedImageUtils.drawTextInImage(mergeImage, new BigDecimal(goodsOrder.getShipperTransportCost().toString()).setScale(2, BigDecimal.ROUND_HALF_UP).toString(), 1050, 2220, new Color(107, 107, 107), 100, "微软雅黑");
            mergeImage = SharedImageUtils.drawTextInImage(mergeImage, DateTimeUtil.dateToString(goodsOrder.getCreateTime()), 1050, 2480, new Color(107, 107, 107), 80, "微软雅黑");
            //二维码位置
            mergeImage = SharedImageUtils.mergeQrcode(mergeImage, qrcodeImage, 830, 2650, 615, 615);
            return ImageEditUtils.getBase64FromImage(mergeImage);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    //车源海报
    @Override
    public String vehicleSHarePosters(TruckVehicle truckVehicle, Account account) {
        try {
            // 获取类所在项目路径，获取方式有多种
            String address = getClass().getResource("/images/").getPath();
            //生成二维码
            BufferedImage qrcodeImage = SharedImageUtils.qrcodeLogo("http://www.baidu.com", address + "truck_logo.png", 180, 180);
            //获取在线图片
            BufferedImage truckImg = SharedImageUtils.getBufferedImageByUrl(truckVehicle.getCommTruckTypeImg());
            BufferedImage mergeImage = ImageIO.read(new File(address + "truck_bg.png"));
            //车辆图片位置
            mergeImage = SharedImageUtils.mergeQrcode(mergeImage, truckImg, 650, 1205, 960, 720);
            //车牌号位置
            mergeImage = SharedImageUtils.drawTextInImage(mergeImage, StringUtil.getStringFirstAndEndReplace(truckVehicle.getTrackNo(), 4, 2, "*"), 450, 2000, new Color(13, 245, 252), 80, true, false, "微软雅黑");
            //车型位置
            mergeImage = SharedImageUtils.drawTextInImage(mergeImage, truckVehicle.getTruckTypeName(), 450, 2120, new Color(107, 107, 107), 50, true, false, "微软雅黑");
            //二维码位置
            mergeImage = SharedImageUtils.mergeQrcode(mergeImage, qrcodeImage, 815, 2300, 615, 615);
            mergeImage = SharedImageUtils.drawTextInImage(mergeImage, account.getUserType().name + "" + account.getName(), 320, 900, new Color(64, 47, 169), 80, Font.BOLD, "黑体");
            return ImageEditUtils.getBase64FromImage(mergeImage);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    //运单海报
    @Override
    public String transitSHarePosters(TransitOrder transitOrder, Account account) {
        try {
            // 获取类所在项目路径，获取方式有多种
            String address = getClass().getResource("/images/").getPath();
            //生成二维码
            BufferedImage qrcodeImage = SharedImageUtils.qrcodeLogo("http://www.baidu.com", address + "transit_logo.png", 180, 180);
            // 1. 通过背景图片构建 BufferedImage
            BufferedImage mergeImage = ImageIO.read(new File(address + "transit_bg.png"));
            mergeImage = SharedImageUtils.drawTextInImage(mergeImage, account.getUserType().name + "" + account.getName(), 320, 900, new Color(64, 47, 169), 80, Font.BOLD, "黑体");
            mergeImage = SharedImageUtils.drawTextInImage(mergeImage, StringUtil.getStringFirstAndEnd(areaCity(transitOrder.getDisplayConsignorArea()), 2, 1, "..."), 440, 1380, new Color(0, 0, 0), 120, "华文行楷");
            mergeImage = SharedImageUtils.drawTextInImage(mergeImage, StringUtil.getStringFirstAndEnd(areaCity(transitOrder.getDisplayConsigneeArea()), 2, 1, "..."), 1320, 1380, new Color(0, 0, 0), 120, "华文行楷");
            mergeImage = SharedImageUtils.drawTextInImage(mergeImage, transitOrder.getTransportNo(), 750, 1737, new Color(104, 104, 109), 90, "微软雅黑");
            mergeImage = SharedImageUtils.drawTextInImage(mergeImage, new BigDecimal(transitOrder.getAssignTransportCost().toString()).setScale(2, BigDecimal.ROUND_HALF_UP).toString(), 750, 1950, new Color(104, 104, 109), 100, "90");
            mergeImage = SharedImageUtils.mergeQrcode(mergeImage, qrcodeImage, 815, 2250, 615, 615);
            return ImageEditUtils.getBase64FromImage(mergeImage);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    //用户海报
    @Override
    public String accountInfoSHarePosters(Organize organize, Account account) {
        try {
            // 获取类所在项目路径，获取方式有多种
            String address = getClass().getResource("/images/").getPath();
            //生成二维码
            BufferedImage qrcodeImage = SharedImageUtils.qrcodeLogo("http://www.baidu.com", address + "account_logo.png", 180, 180);
            //获取在线图片
            BufferedImage truckImg = SharedImageUtils.getBufferedImageByUrl(organize.getOrganizeLogo() == null ? "https://picsum.photos/200/300" : organize.getOrganizeLogo());
            //图片圆形
            truckImg = SharedImageUtils.convertRoundedImage(truckImg, 100);

            BufferedImage mergeImage = ImageIO.read(new File(address + "account_bg.png"));
            mergeImage = SharedImageUtils.drawTextInImage(mergeImage, account.getUserType().name + "" + account.getName(), 320, 900, new Color(64, 47, 169), 80, Font.BOLD, "黑体");

            //用户头像图片位置
            mergeImage = SharedImageUtils.mergeQrcode(mergeImage, truckImg, 933, 1395, 350, 350);
            //名称位置
            mergeImage = SharedImageUtils.drawTextInImage(mergeImage, organize.getOrganizeName(), 450, 1900, new Color(0, 0, 0), 100, true, false, "华文行楷");
            //车型位置
            mergeImage = SharedImageUtils.drawTextInImage(mergeImage, organize.getUserType().name, 450, 2050, new Color(0, 0, 0), 80, true, false, "华文行楷");
            //二维码位置
            mergeImage = SharedImageUtils.mergeQrcode(mergeImage, qrcodeImage, 815, 2300, 615, 615);
            return ImageEditUtils.getBase64FromImage(mergeImage);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 生成APP图标
     * appType: 'shipper', //app类型
     * themeColor: '#e47a00', //主题
     * bgColor: '#ffffff', //导出图标背景色
     * iconRadius: 0, //导出图标圆角
     * iconWidth: 260, //导出图标宽度
     * iconHeight: 260, //导出图标高度
     * markbase64: null, //标签BASE64
     * logobase64: null, //logoBASE64
     * logoWidth: 0, //logo图片宽度
     * logoHeight: 0, //logo图片高度
     * logoLeft: 0, //logo左侧坐标
     * logoTop: 0, //logo顶部坐标
     */
    @Override
    public String generateAppIcon(AppIcon appIcon) {
        BufferedImage logoBase64 = ImageEditUtils.base64ToBufferedImage(appIcon.getLogobase64());
        BufferedImage markBase64 = ImageEditUtils.base64ToBufferedImage(appIcon.getMarkbase64());
        if (markBase64 != null) markBase64 = ImageEditUtils.createRoundImage(markBase64, appIcon.getIconRadius().intValue());
        // 1.创建空白图片
        BufferedImage mainImage = new BufferedImage(appIcon.getIconWidth().intValue(), appIcon.getIconHeight().intValue(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D graphic = (Graphics2D) mainImage.getGraphics();
        // 设置抗锯齿
        graphic.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphic.setColor(new Color(0,0,0,0));
        graphic.fillRect(0, 0, appIcon.getIconWidth().intValue(), appIcon.getIconHeight().intValue());
        if (appIcon.getBgColor() != null){
            graphic.setColor(new Color(ImageEditUtils.parseHex(appIcon.getBgColor())));
            graphic.fillRoundRect(0, 0, appIcon.getIconWidth().intValue(), appIcon.getIconHeight().intValue(), appIcon.getIconRadius().intValue()*2, appIcon.getIconRadius().intValue()*2);
        }
        // 2.绘制LOGO
        graphic.drawImage(logoBase64, appIcon.getLogoLeft().intValue(), appIcon.getLogoTop().intValue(), appIcon.getLogoWidth().intValue(), appIcon.getLogoHeight().intValue(), null);
        if (markBase64 != null) graphic.drawImage(markBase64, 0, 0, appIcon.getIconWidth().intValue(), appIcon.getIconHeight().intValue(), null);
        graphic.dispose();
        return ImageEditUtils.getBase64FromImage(mainImage);
    }

    //截取地址
    private String areaCity(String area) {
        return area.substring(area.indexOf("-") + 1, area.lastIndexOf("-"));
    }


}
